<?php

namespace app\app\controller;

use app\app\model\BiddingGovMaterialModel;
use app\BaseController;
use think\db\exception\DbException;

class BiddingGovMaterial extends BaseController
{
    private $model;

    /**
     * initialize
     * @return void
     */
    public function initialize()
    {
        $this->model = BiddingGovMaterialModel::class;
    }

    /**
     * C&U
     * @return void
     */
    public function save()
    {
        $id = input('id', '');
        $param = $this->request->param();

        if (empty($id)) {
            $model = new $this->model();
        } else {
            $model = $this->model::where('id', $id);
        }

        $model->save($param);

        $this->success('success');
    }

    /**
     * R
     * @return void
     * @throws DbException
     */
    public function list()
    {
        $where = [];

        $page_size = input('pageSize', '20');

        $id = input('bidding_id', '');
        empty($id) && $this->error('请传入id');
        $where[] = ['bidding_id', '=', $id];

        // 搜索
        $number = input('number', '');
        if (!empty($number)) $where[] = ['number', 'like', '%' . $number . '%'];

        $project_name = input('project_name', '');
        if (!empty($project_name)) $where[] = ['project_name', 'like', '%' . $project_name . '%'];

        $name = input('name', '');
        if (!empty($name)) $where[] = ['name', 'like', '%' . $name . '%'];

        $bidding_time = input('bidding_time', '');
        if (!empty($bidding_time)) {
            $where[] = ['end_time', '>=', $bidding_time];
            $where[] = ['start_time', '<=', $bidding_time];
        }

        $data = $this->model::where($where)
            ->paginate($page_size);

        empty($data) && $this->error('查询为空');

        $this->success('success', $data->toArray());
    }

    /**
     * D
     * @return void
     * @throws DbException
     */
    public function del($id = 0)
    {
        !$id && $this->error('请传入id');

        $this->model::where('id', $id)->delete();

        $this->success('success');
    }
}